package com.api.Fallback;

import com.api.ItemClient;
import com.cart.common.exception.BadRequestException;
import com.cart.common.utils.CollUtils;
import com.dto.ItemDTO;
import com.dto.OrderDetailDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;



/**
 * @author: LiuLi
 * @Date: 2025/09/03 16:52
 * @Description:
 */
@Slf4j
@Component
public class ItemClientFallback implements FallbackFactory<ItemClient> {

    @Override
    public ItemClient create(Throwable cause) {
        return new ItemClient() {

            @Override
            public List<ItemDTO> queryItemByIds(Collection<Long> ids) {
                log.error("查询购物车商品信息时远程调用ItemClient#queryItemByIds方法出现异常，参数：{}", ids, cause);
                cause.getMessage();
                // 查询购物车允许失败，查询失败，返回空集合
                return new ArrayList<>();
            }

            @Override
            public void deductStock(List<OrderDetailDTO> items) {
                log.error("批量扣减库存时远程调用ItemClient#deductStock方法出现异常，参数：{}", items, cause);
                // 批量扣减库存允许失败,抛出异常
                throw new BadRequestException("批量扣减库存失败");
            }
        };
    }
}
